<?php
class KML
{
	function getKML( $name, $coords ){
		$tmpStr = "";
		$tmpStr .= $this->getFileheader();
		$tmpStr .=  $this->getDocHeader();
		$tmpStr .=  $this->getStyle();

		$tmpStr .= $this->getPlacemarkHeader("Test", "genericStyle");
		
		$tmpStr .=  $this->getPolygonHeader();
		$tmpStr .=  $this->getCoordinates($coords);
		$tmpStr .=  $this->getPolygonFooter();
		
		$tmpStr .=  $this->getPlacemarkFooter();
		
		$tmpStr .=  $this->getDocFooter();
		$tmpStr .=  $this->getFileFooter();
		
		return $tmpStr;
	}
	
	function getOuterBoundaryHeader(){
		return "<outerBoundaryIs><LinearRing>";
	}
	function getOuterBoundaryFooter(){
		return "</LinearRing></outerBoundaryIs>";
	}	
	function getPolygonHeader(){
		return "<Polygon>\n" . 
			"<tessellate>1</tessellate>\n" .
			"<outerBoundaryIs>\n" .
			"<LinearRing>";
	}
	function getPolygonFooter(){
		return "</LinearRing>\n" .
			"</outerBoundaryIs>\n" .
			"</Polygon>\n";
	}
	function getCoordinates($coords){
		return "<coordinates>$coords</coordinates>";
	}

	function getPlacemarkHeader($name, $style){
		return "<Placemark><name>$name</name><styleUrl>#$style</styleUrl>";
	}
	function getPlacemarkFooter(){
		return "</Placemark>";
	}
	function getFileHeader(){
		return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://earth.google.com/kml/2.2\">";
	}
	
	function getFileFooter(){
		return "</kml>";
	}
	
	function getDocHeader(){
		return '<Document><name>AutoGenerated KML</name>\n';
	}
	
	function getDocFooter(){
		return '</Document>';
	}
	
	function getStyle(){
		// Build style
		$styleStr = "<Style id=\"genericStyle\">\n";
		$styleStr .= $this->getLineStyle(5, "ff0000ff");
		$styleStr .= $this->getPolyStyle(.25, "0000ff", 1);
		$styleStr .= "</Style>\n";
		return $styleStr;
	}
	function getPolyStyle($opacity, $color, $filled){
		$polyStr = "<PolyStyle>\n";
		$polyStr .= "<color>40$color</color>";
		$polyStr .= "</PolyStyle>\n";
		return $polyStr;
	}
	function getLineStyle( $width, $color){
		return "<LineStyle><color>$color</color><width>$width</width></LineStyle>\n";
	}
	
}
?>